import { Getter, GetterTree } from 'vuex'
import { State, CartProduct } from'./index'
// 全局的getter, 获取购物车的数据，因为加入购物车的时候，payload只是传递id
// 所以需要从商品列表中找出当前商品的名称和价格
// 真实场景应该不需要再查一遍，直接传递整个商品信息
const cartProducts: Getter<State, any> = (state: State) => {
    return state.cart.added.map((shape) => {
        const product = state.products.all.find((p) => p.id === shape.id);
        if (product) {
            const cartProduct: CartProduct = {
                title: product.title,
                price: product.price,
                quantity: shape.quantity
            }
            return cartProduct
        }

    })
};

const getterTree: GetterTree<State, any> = {
    cartProducts
};

export default getterTree
